//Documentation complete
/**
 * @author Kazu
 * @version 2009AP05
 */

package control;

import problemDomain.*;
import static problemDomain.ErrorBean.ErrorCode.*;
import broker.*;

import java.util.List;

/**
 * Control object for delivery object operations.
 */
public class DeliveryControl extends Control {
	
	/* Attributes */
	
	private DeliveryBroker db;
	private ConfigFileBroker cfb;
	private OrderBroker ob;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>DeliveryControl</code>.
	 * @throws ErrorBean if the database is not available
	 */
	public DeliveryControl () throws ErrorBean {
		db = DeliveryBroker.getBroker();
		cfb = ConfigFileBroker.getBroker();
		ob = OrderBroker.getBroker();
	}
	
	/* END Constructors */
	
	
	/* Methods */
	
	/**
	 * Creates a new delivery.
	 * @param firstname the first name on the delivery
	 * @param lastname the last name on the delivery
	 * @param phone the phone number on the delivery
	 * @param address the street address of the delivery
	 * @param city the city of the delivery address
	 * @param province the province of the delivery address
	 * @param postalCode the postal code of the delivery address
	 * @param dwellingType the dwelling type to delivery to
	 * @param building the building name on the delivery
	 * @param room the room number of the delivery address
	 * @param status the status of the delivery
	 * @param instruction additional delivery instructions
	 * @param orderID the ID of the order with which this delivery is associated
	 * @throws ErrorBean if there is an error creating the delivery
	 */
	public void create (String firstname, String lastname, String phone,
						String address, String city, String province, String postalCode, 
						String dwellingType, String building, String room, String status,
						String instruction, String orderID) throws ErrorBean {
		DeliveryBean del = new DeliveryBean(0, firstname, lastname, phone, address, city, province, postalCode, 
				dwellingType, building, room, status, instruction);
		db.persist(del);
		
		// make the link between the newly created delivery and its order
		int deliveryID = db.getNewId();
		OrderBroker ob = OrderBroker.getBroker();
		ob.addDelivery(new OrderBean(checkOrderId(orderID)), new DeliveryBean(deliveryID));
	}
	
	/**
	 * Searches for deliveries which match the specified criteria.
	 * @param id the ID to search for (0 excludes this field when searching)
	 * @param firstname the first name to search for (<code>""</code> excludes this field when searching)
	 * @param lastname the last name to search for (<code>""</code> excludes this field when searching)
	 * @param phone the phone number to search for (<code>""</code> excludes this field when searching)
	 * @return a <code>List</code> of results
	 * @throws ErrorBean if the input data are not correct
	 */
	public List<DeliveryBean> search (String id, String firstname, String lastname, String phone) throws ErrorBean {
		int idInt = 0;
		if (id != null && !id.equals(""))
			idInt = checkDeliveryId(id);
		
		if (idInt > 0)
			return db.search(new DeliveryBean(idInt));
		
		DeliveryBean searchBean = new DeliveryBean();
		searchBean.setFirstname(firstname);
		searchBean.setLastname(lastname);
		searchBean.setPhone(phone);
		return db.search(searchBean);
	}
	
	/**
	 * Updates a delivery.
	 * @param id the ID of the delivery
	 * @param firstname the first name on the delivery
	 * @param lastname the last name on the delivery
	 * @param phone the phone number on the delivery
	 * @param address the street address of the delivery
	 * @param city the city of the delivery address
	 * @param province the province of the delivery address
	 * @param postalCode the postal code of the delivery address
	 * @param dwelling the dwelling type to delivery to
	 * @param building the building name on the delivery
	 * @param room the room number of the delivery address
	 * @param status the status of the delivery
	 * @param instruction additional delivery instructions
	 * @throws ErrorBean if the input data are not correct
	 */
	public void update (String id, String firstname, String lastname,
			String phone, String address, String city, String province,
			String postalCode, String dwelling, String building, String room,
			String status, String instruction) throws ErrorBean {
		DeliveryBean del = new DeliveryBean(checkDeliveryId(id), firstname, lastname, 
									phone, address, city, province, postalCode, dwelling,
									building, room, status, instruction);
		db.persist(del);
	}
	
	/**
	 * Deletes a delivery.
	 * @param id the ID of the delivery
	 * @throws ErrorBean if the input data are not correct
	 */
	public void delete (String id) throws ErrorBean {
		DeliveryBean delivery = new DeliveryBean(checkDeliveryId(id));
		ob.deleteDelivery(delivery);
		db.remove(delivery);
	}
	
	
	/**
	 * Lists all deliveries.
	 * @return a <code>List</code> of the results
	 * @throws ErrorBean if there is an error retrieving the data
	 */
	public List<DeliveryBean> listAll () throws ErrorBean {
		return db.listAll();
	}
	
	/**
	 * Lists all dwelling types.
	 * @return a <code>List</code> of the results
	 * @throws ErrorBean if there is an error retrieving the data
	 */
	public List<String> listDwellingType () throws ErrorBean	{
		return cfb.listAll("dwellingType");
	}
	
	/**
	 * Lists all delivery statuses.
	 * @return a <code>List<code> of the results
	 * @throws ErrorBean if there is an error retrieving the data
	 */
	public List<String> listDeliveryStatus() throws ErrorBean {
		return cfb.listAll("deliveryStatus");
	}
	
	/* END Methods */
	
	
	/* Data Checks */
	
	private int checkOrderId (String id) throws ErrorBean {
		return checkInt(id, ORDER_ID_INVALID, this);}
	
	private int checkDeliveryId (String id) throws ErrorBean {
		return checkInt(id, DELIVERY_ID_INVALID, this);}
	
	/* END Data Checks */
	
}
